package com.xiaochuan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaochuan.pojo.UserRole;
import com.xiaochuan.service.UserRoleService;
import com.xiaochuan.utils.ResultBody;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 经典
 * @since 2021-04-21
 */
@Controller
@RequestMapping("user-role")
public class UserRoleController {
    public static final Logger logger = LoggerFactory.getLogger(UserRoleController.class);

    @Resource
    private UserRoleService userRoleService;

    /*
     * 跳转角色页面
     * */
    @RequestMapping(value = "findrole", produces = {"application/json;charset=UTF-8"})
    @RequiresPermissions(value = {"sys:role:list"})
    public String findrole() {
        logger.info("-------------->跳转角色页面");
        return "/user_role/list_role";
    }


    /*
     * 跳转修改页面
     * */
    @RequestMapping(value = "updaterole", produces = {"application/json;charset=UTF-8"})
    @RequiresPermissions(value = {"sys:role:update"})
    public String updaterole() {
        logger.info("-------------->跳转角色修改页面");
        return "/user_role/update_role";
    }

    /*
     * 全查询
     * */
    @RequestMapping("roleList")
    @RequiresPermissions(value = {"sys:role:find"})
    @ResponseBody
    public Object roleList(
            @RequestParam(value = "roleName", required = false) String roleName,
            @RequestParam(value = "page", defaultValue = "0") Integer page,
            @RequestParam(value = "limit", defaultValue = "20") Integer limit
    ) {
        logger.info("-------------->查询角色页面");
        logger.info("-------------->参数 :page" + page + "  --->limit:" + limit + "  --->roleName:" + roleName);

        Page<UserRole> UserRolepage = userRoleService.page(new Page<>(page, limit), roleName == null ? null : new QueryWrapper<UserRole>().like("role_name", roleName));
        return ResultBody.success(UserRolepage);

    }

    /*
     * 修改
     * */
    @RequestMapping(value = "update_role", produces = {"application/json;charset=UTF-8"})
    @RequiresPermissions(value = {"sys:role:update"})
    @ResponseBody
    public Object update_role(@RequestBody UserRole userRole) {
        logger.info("-------------->角色修改");
        logger.info("-------------->参数 :userRole" + userRole);

        if (null != userRole.getRoleName() && null != userRole.getRoleId()) {
            boolean update = userRoleService.update(userRole, new UpdateWrapper<UserRole>()
                    .eq("role_id", userRole.getRoleId())
            );
            if (update) {
                return ResultBody.success();
            }
            return ResultBody.error("修改失败");
        } else {
            return ResultBody.error("参数为空");
        }

    }

    /*
     * 删除
     * */
    @RequestMapping(value = "delete_role", produces = {"application/json;charset=UTF-8"})
    @RequiresPermissions(value = {"sys:role:delete"})
    @ResponseBody
    public Object delete_role(@RequestBody UserRole userRole) {
        logger.info("-------------->角色删除");
        logger.info("-------------->删除参数 :userRole" + userRole);
        if (null != userRole.getRoleId()) {
            boolean b = userRoleService.removeById(userRole.getRoleId());
            if (b) {
                return ResultBody.success();
            }
            return ResultBody.error("删除失败");
        } else {
            return ResultBody.error("参数为空");
        }

    }

}

